package com.hengyue.common.utils;

import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.security.Key;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;

public class RSAUtils {
    /**
     * 类型
     */
    public static final String ENCRYPT_TYPE = "RSA";

    public static String getPublicKey(String filename) throws Exception {
        //默认UTF-8 编码，可以在构造中传入第二个参数做为编码
        FileReader fileReader = new FileReader(filename);
        return fileReader.readString();
    }

    public static String getPrivateKey(String filename) {
        Resource resource_2 = new ClassPathResource("rsa/privateKey.txt");
        //默认UTF-8 编码，可以在构造中传入第二个参数做为编码
        FileReader fileReader = new FileReader(filename);
        return fileReader.readString();
    }

    public static String getPrivateKey() throws IOException {
        Resource resource = new ClassPathResource("rsa/privateKey.txt");
        String filename = resource.getFile().getPath();
        return getPrivateKey(filename);
    }

    /**
     * 公约加密
     *
     * @param content   加密内容
     * @param publicKey 公约
     * @return /
     */
    public static String encrypt(String content, PublicKey publicKey) {
        try {
            RSA rsa = new RSA(null, publicKey);
            return rsa.encryptBase64(content, KeyType.PublicKey);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 公约加密
     *
     * @param content   加密内容
     * @param publicKey 字符串公约
     * @return /
     */
    public static String encrypt(String content, String publicKey) {
        try {
            RSA rsa = new RSA(null, publicKey);
            return rsa.encryptBase64(content, KeyType.PublicKey);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 私钥解密
     *
     * @param content    要解密的内容
     * @param privateKey 私钥
     */
    public static String decrypt(String content, PrivateKey privateKey) {
        try {
            RSA rsa = new RSA(privateKey, null);
            return rsa.decryptStr(content, KeyType.PrivateKey);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 私钥解密
     *
     * @param content    要解密的内容
     * @param privateKey 私钥
     */
    public static String decrypt(String content, String privateKey) {
        try {
            RSA rsa = new RSA(privateKey, null);
            return rsa.decryptStr(content, KeyType.PrivateKey);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取公私钥-请获取一次后保存公私钥使用
     *
     * @param publicKeyFilename  公钥生成的路径
     * @param privateKeyFilename 私钥生成的路径
     */
    public static void generateKeyPair(String publicKeyFilename, String privateKeyFilename) {
        try {
            KeyPair pair = SecureUtil.generateKeyPair(ENCRYPT_TYPE);
            PrivateKey privateKey = pair.getPrivate();
            PublicKey publicKey = pair.getPublic();
            // 获取 公钥和私钥 的 编码格式（通过该 编码格式 可以反过来 生成公钥和私钥对象）
            byte[] pubEncBytes = publicKey.getEncoded();
            byte[] priEncBytes = privateKey.getEncoded();

            String pubEncBase64 = Base64Encoder.encode(pubEncBytes);
            String priEncBase64 = Base64Encoder.encode(priEncBytes);

            FileWriter pub = new FileWriter(publicKeyFilename);
            FileWriter pri = new FileWriter(privateKeyFilename);
            pub.write(pubEncBase64);
            pri.write(priEncBase64);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
